package com.sensetime.jv.util;

import com.sensetime.jv.enums.StatusEnum;
import com.sensetime.jv.exception.CustomException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 断言工具类，复用 Spring AssertUtils 的全部 API，
 * 但将异常统一抛出为 {@link CustomException}。
 *
 * <p>所有方法均与 Spring AssertUtils 同名同参，
 * 只是额外支持传入 {@link StatusEnum} 以指定错误码。
 * 只是额外支持传入 {@link org.springframework.util.AssertUtils} 以指定错误码。
 */
public abstract class AssertUtil {

    /* ======================== 字符串相关 ======================== */

    public static void hasText(@Nullable String text, StatusEnum status) {
        if (!StringUtils.hasText(text)) {
            throw new CustomException(status);
        }
    }

    public static void hasText(@Nullable String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new CustomException(message);
        }
    }

    public static void hasText(@Nullable String text, String message, StatusEnum status) {
        if (!StringUtils.hasText(text)) {
            throw new CustomException(message, status);
        }
    }

    /* ======================== 对象非空 ======================== */

    public static void notNull(@Nullable Object object, StatusEnum status) {
        if (object == null) {
            throw new CustomException(status);
        }
    }

    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            throw new CustomException(message);
        }
    }

    public static void notNull(@Nullable Object object, String message, StatusEnum status) {
        if (object == null) {
            throw new CustomException(message, status);
        }
    }

    /* ======================== 数组 / 集合非空 ======================== */

    public static void notEmpty(@Nullable Object[] array, StatusEnum status) {
        if (ObjectUtils.isEmpty(array)) {
            throw new CustomException(status);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, StatusEnum status) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new CustomException(status);
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, StatusEnum status) {
        if (CollectionUtils.isEmpty(map)) {
            throw new CustomException(status);
        }
    }

    /* ======================== 布尔表达式 ======================== */

    public static void isTrue(boolean expression, StatusEnum status) {
        if (!expression) {
            throw new CustomException(status);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new CustomException(message);
        }
    }

    public static void isTrue(boolean expression, String message, StatusEnum status) {
        if (!expression) {
            throw new CustomException(message, status);
        }
    }

    /* ======================== 其它常用断言（按需扩展） ======================== */

    public static void doesNotContain(@Nullable String textToSearch, String substring, StatusEnum status) {
        if (StringUtils.hasLength(textToSearch)
                && StringUtils.hasLength(substring)
                && textToSearch.contains(substring)) {
            throw new CustomException(status);
        }
    }


}